/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ipcc.report.entity;

import com.ipcc.manager.entity.IPCC_EXTENSIONS;
import com.ipcc.manager.entity.IPCC_EXTENSION_GROUPS;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import telsoft.admin.util.AppServer;
import telsoft.sql.Database;

/**
 *
 * @author Administrator
 */
public class SUM_FS_CDR implements Serializable {

    private String sum_date;
    private String extension;
    private int missed_calls;
    private int answered_calls;
    private int rejected_calls;
    private float total_answer_duration;
    private float average_answer_duration;
    private float average_response_duration;
    private float total_response_duration;

    public SUM_FS_CDR() {
    }

    public String getSum_date() {
        return sum_date;
    }

    public void setSum_date(String sum_date) {
        this.sum_date = sum_date;
    }

    public String getExtension() {
        return extension;
    }

    public void setExtension(String extension) {
        this.extension = extension;
    }

    public int getMissed_calls() {
        return missed_calls;
    }

    public void setMissed_calls(int missed_calls) {
        this.missed_calls = missed_calls;
    }

    public int getAnswered_calls() {
        return answered_calls;
    }

    public void setAnswered_calls(int answered_calls) {
        this.answered_calls = answered_calls;
    }

    public int getRejected_calls() {
        return rejected_calls;
    }

    public void setRejected_calls(int rejected_calls) {
        this.rejected_calls = rejected_calls;
    }

    public float getTotal_answer_duration() {
        return total_answer_duration;
    }

    public void setTotal_answer_duration(float total_answer_duration) {
        this.total_answer_duration = total_answer_duration;
    }

    public float getAverage_answer_duration() {
        return average_answer_duration;
    }

    public void setAverage_answer_duration(float average_answer_duration) {
        this.average_answer_duration = average_answer_duration;
    }

    public float getAverage_response_duration() {
        return average_response_duration;
    }

    public void setAverage_response_duration(float average_response_duration) {
        this.average_response_duration = average_response_duration;
    }

    public float getTotal_response_duration() {
        return total_response_duration;
    }

    public void setTotal_response_duration(float total_response_duration) {
        this.total_response_duration = total_response_duration;
    }    

    public static ArrayList<SUM_FS_CDR> getAll(String condition, String chitiet) throws Exception {
        ArrayList<SUM_FS_CDR> list = new ArrayList<SUM_FS_CDR>();
        Connection cn = null;
        Statement st = null;
        ResultSet rs = null;
        String strSQL = "";
        if (chitiet.equals("mm")) {
            strSQL = "select extension,sum(missed_calls),sum(answered_calls),sum(rejected_calls),sum(total_answer_duration),"
                    + " sum(average_answer_duration),sum(average_response_duration),sum(total_response_duration)"
                    + " from sum_fs_cdr"
                    + " " + condition
                    + " group by extension"
                    + " order by extension";
        } else {
            strSQL = "select to_char(sum_date,'dd-mm-yyyy') ngay,extension,missed_calls,answered_calls,rejected_calls,total_answer_duration,"
                    + " average_answer_duration,average_response_duration,total_response_duration"
                    + " from sum_fs_cdr"
                    + " " + condition
                    + " order by sum_date,extension";
        }
        try {
            cn = AppServer.getConnection();
            st = cn.createStatement();
            rs = st.executeQuery(strSQL);
            while (rs.next()) {
                SUM_FS_CDR sfc = new SUM_FS_CDR();
                int j = 1;
                if (!chitiet.equals("mm")) {
                    j = 2;
                    sfc.setSum_date(rs.getString(1));
                }
                sfc.setExtension(rs.getString(j));
                for (int i = j + 1; i < j + 8; i++) {
                    String strRs = "0";
                    if (rs.getString(i) != null && !rs.getString(i).equalsIgnoreCase("")) {
                        strRs = rs.getString(i);
                    }
                    if (i == j + 1) {
                        sfc.setMissed_calls(Integer.parseInt(strRs));
                    }
                    if (i == j + 2) {
                        sfc.setAnswered_calls(Integer.parseInt(strRs));
                    }
                    if (i == j + 3) {
                        sfc.setRejected_calls(Integer.parseInt(strRs));
                    }
                    if (i == j + 4) {
                        sfc.setTotal_answer_duration(Float.parseFloat(strRs));
                    }
                    if (i == j + 5) {
                        sfc.setAverage_answer_duration(Float.parseFloat(strRs));
                    }
                    if (i == j + 6) {
                        sfc.setAverage_response_duration(Float.parseFloat(strRs));
                    }
                    if (i == j + 7) {
                        sfc.setTotal_response_duration(Float.parseFloat(strRs));
                        if (!chitiet.equals("mm") && sfc.getAnswered_calls() > 0) {
                            float asd = sfc.getTotal_answer_duration() / sfc.getAnswered_calls();
                            sfc.setAverage_answer_duration(asd);
                            
                            float ard = sfc.getTotal_response_duration() / sfc.getAnswered_calls();
                            sfc.setAverage_response_duration(ard);
                        }                        
                    }
                }
                list.add(sfc);
            }
            return list;
        } finally {
            Database.closeObject(rs);
            Database.closeObject(st);
            Database.closeObject(cn);
        }
    }
        
}
